package com.utilities.visualiser.circularContigs;

import java.io.FileReader;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Element;

import com.utilities.IO.FileUtilities;
import com.utilities.parsers.ContigParser.ParseAllContigsFileParser;
import com.utilities.parsers.ContigParser.ParseEvents;
import com.utilities.parsers.ContigParser.ParseListener;
import com.utilities.visualiser.PrepareXMLFile;

public class ContigVisualiser extends ParseListener {
	
	String contigName = null;
	int endIndex = 1;
	Element directScoreFeature = null;
	String seqLength = null;
	PrepareXMLFile prepareXMLFile = null;
	String radiusAdjustment = null;
	String featureSlotId = null;
	String featureColor = null;
	String[] colors = {"maroon","red","blue","green","orange", "purple","fuchsia","navy",
			"lime","olive","yellow","teal","aqua","silver","gray","white","black"};
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception
	{
		args = new String[8];
//		args[0] = "4639675"; args[1] = "c:/database/454/7328Run1.fna" ;
//		args[2] = "c:/database/454/7328Run2.fna"; args[3] = "c:/database/454/Gamma22Run2.fna" ;
//		args[4] = "c:/database/454/Gamma22Run2.fna"; args[5] = "c:/database/454/Gamma52Run1.fna" ;
//		args[6] = "c:/database/454/Gamma52Run2.fna"; args[7] = "c:/database/454/Gamma70Run1.fna" ;
		
		args[0] = "4639675"; args[1] = "c:/database/454/7328Run1_gaps.fna" ;
		args[2] = "c:/database/454/7328Run2_gaps.fna"; args[3] = "c:/database/454/Gamma22Run2_gaps.fna" ;
		args[4] = "c:/database/454/Gamma22Run2_gaps.fna"; args[5] = "c:/database/454/Gamma52Run1_gaps.fna" ;
		args[6] = "c:/database/454/Gamma52Run2_gaps.fna"; args[7] = "c:/database/454/Gamma70Run1_gaps.fna" ;
		
		
		ContigVisualiser contigParser = new ContigVisualiser();
		contigParser.prepareXMLFile = new PrepareXMLFile().initialise();
		contigParser.makeFeatureSlots(contigParser.initialise(args[0],args), args.length - 1);
		
		for (int i = 1; i < args.length; i++)
		{
			ParseAllContigsFileParser allContigsFileParser = 
				new ParseAllContigsFileParser();
			contigParser.featureColor = contigParser.colors[i-1];
			contigParser.featureSlotId = "directStrand" + i;
//			if (i%2 == 0)
//				contigParser.featureColor = "red";
//			else
//				contigParser.featureColor = "blue";
			allContigsFileParser.addListener(contigParser);
			allContigsFileParser.parse(new FileReader(args[i]));
			allContigsFileParser.removeAllListeners();
		}
		contigParser.prepareXMLFile.writeToFile("c:/database/454LargeContigs.xml");
		
	}

	public void handleEvent(ParseEvents event) 
	{
		try
		{
			switch((ParseEvents.EventTypes)event.getEventType())
			{
				case docStarted : 
					break;
				case docEnded : System.out.println(" end reading " + contigName); endIndex = 1; break; 
				case contigStarted: 
				{
					if (endIndex != 1)
					{
						System.out.println(" end reading " + contigName);
					}
					
					String str = (String)event.getSource();
					String[] params = str.split(" ");
					contigName = params[0].split(">")[1];
					System.out.print("started reading " + contigName);
					int newStartIndex = 0;
					for (String param : params)
					{
						if (param.contains(".."))
						{
							newStartIndex = Integer.parseInt(param.split("[..]")[0].trim());
							if (endIndex < newStartIndex)
							{
								endIndex = Integer.parseInt(param.split("[..]")[2].trim());
								String mouseOver = "Start=" + newStartIndex + " End=" + endIndex;
								Element feature = prepareXMLFile.createFeature(featureSlotId, featureColor, "clockwise-arrow", 
										contigName, null,mouseOver,null,null,radiusAdjustment);
								prepareXMLFile.createFeatureRange(feature, String.valueOf(newStartIndex), String.valueOf(endIndex), null);
							}
							else
							{
								System.out.println("Complement found at : " + contigName);
							}
							
							break;
						}
					}
					break;
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	private PrepareXMLFile initialise(String seqLength, String[] args) throws ParserConfigurationException 
	{
		PrepareXMLFile result = prepareXMLFile.createCGView(seqLength)
		.createLegend("sideLegend", "upper-right", "SanSerif, plain, 10", "0.8");
		
		for (int i = 1; i < args.length; i++)
		{
			String legendLabel = FileUtilities.removeExtension(FileUtilities.getAbstractPath(args[i]));
			result = result.createLegendItem("sideLegend", legendLabel, "SanSerif, plain, 12", true, this.colors[i - 1]);
		}
		result = result.createLegend("lowerleftlegend", "lower-left", "SanSerif, plain, 9", "0.8")
		.createLegendItem("lowerleftlegend", "This map was generated using CGView", null, false, null)
		.createLegend("lowerrightlegend", "lower-right", "SanSerif, plain, 9", "0.8")
		.createLegendItem("lowerrightlegend", "Work done at D-BSSE ETH Zurich", null, false, null);
		return result;
	}
	
	private PrepareXMLFile makeFeatureSlots(PrepareXMLFile prepareXMLFile, int slots) 
	{
		PrepareXMLFile result = prepareXMLFile;
		for(int i = 1; i <= slots; i++)
		{
			result = result.createFeatureSlots("directStrand" + i, "direct",false);
		}
		return result;
	}
}
